import { Avatar, Button, List, message } from "antd";
import ProblemManageKnowledge from "../modal/ProblemManageKnowledge";
import ProblemContent from "./ProblemContent";
import ProblemKnowledgePreview from "./ProblemKnowledgePreview";
import { FC } from "react";
import { httpPost } from "@utils/HttpUtil";

const deleteProblem = (problemId:any)=>{
  var status = httpPost("/problem/deleteProblem", {
    problemId: problemId
  }).data;
  if(status){
    message.success("删除成功");
  }else{
    message.error("删除失败");
  }
}

const PreviewProblemTable: FC<any> = ({ data }) => {
  return (
    <>
      <List
        itemLayout="vertical"
        pagination={{
          onChange: (page) => {
            console.log(page);
          },
          pageSize: 3,
        }}
        dataSource={data}
        renderItem={(item: any) => {
          var knowledgeList = httpPost("/problemKnowledge/getProblemKnowledgeList", {
            problemId: item.content.id,
          }).data;
          return (
            <List.Item
              style={{ margin: 20, width: 600 }}
              key={item.id}
              actions={[
                <ProblemManageKnowledge
                  knowledgeList={knowledgeList}
                  problemId={item.content.id}
                />,
                <Button
                  type="link"
                  onClick={() => {
                    deleteProblem(item.content.id);
                  }}
                >
                  删除题目
                </Button>,
              ]}
            >
              <List.Item.Meta
                avatar={<Avatar src={item.avatar} onClick={() => {}} />}
                description={<a>{"上传用户：" + item.description}</a>}
                title={<a>{item.title}</a>}
              />
              <ProblemContent data={item.content} />
              <ProblemKnowledgePreview knowledgeList={knowledgeList} />
            </List.Item>
          );
        }}
      />
    </>
  );
};

export default PreviewProblemTable;